/*
 * Copyright 2005-2013 shopxx.net. All rights reserved.
 * Support: http://www.shopxx.net
 * License: http://www.shopxx.net/license
 * 
 * JavaScript - lSelect
 * Version: 3.0
 */

(function($) {
	$.fn.extend({
		lSelect: function(options) {
			var settings = {
				choose: "请选择",					//默认的option
				emptyValue: "",						//默认option的值
				cssStyle: {"margin-right": "4px"},	//给select选择框添加的样式
				attr: {},							//给select选择框添加的属性集合(例如控制数据验证操作等)
				url: null,							//接口地址
				type: "GET",						//http请求方式
				parentId:null						//可以传递省的id号，用于控制只显示指定省下的区域，广东省id为：1946
			};
			$.extend(settings, options);
			
			var cache = {};
			return this.each(function() {
				var $input = $(this);
				var id = $input.val();
				var treePath = $input.attr("treePath");
				var selectName = $input.attr("name") + "_select";
				
				if (treePath != null && treePath != "") {
					
					//用于控制传递了parentId后，不显示parentId同级跟上级选择框
					if(settings.parentId!=null && settings.parentId!=''){
						var parentIdIndex=treePath.indexOf(settings.parentId);
						if(parentIdIndex>=0){
							treePath=treePath.substring(parentIdIndex);
						}
					}
					
					var ids = (treePath + id + ",").split(",");
					var $position = $input;
					for (var i = 1; i < ids.length; i ++) {
						$position = addSelect($position, ids[i - 1], ids[i]);
					}
				} else {
					addSelect($input, settings.parentId, null);
				}
				
				function addSelect($position, parentId, currentId) {
					$position.nextAll("select[name='" + selectName + "']").remove();
					if ($position.is("select") && (parentId == null || parentId == "")) {
						return false;
					}
					if (cache[parentId] == null) {
						$.ajax({
							url: settings.url,
							type: settings.type,
							data: parentId != null ? {parentId: parentId} : null,
							dataType: "json",
							cache: false,
							async: false,
							success: function(data) {
								cache[parentId] = data;
							}
						});
					}
					var data = cache[parentId];
					if ($.isEmptyObject(data)) {
						return false;
					}
					var select = '<select class="form-control area" name="' + selectName + '">';
					if (settings.emptyValue != null && settings.choose != null) {
						select += '<option value="' + settings.emptyValue + '">' + settings.choose + '</option>';
					}
					$.each(data, function(value, name) {
						if(value == currentId) {
							select += '<option value="' + value + '" selected="selected">' + name + '</option>';
						} else {
							select += '<option value="' + value + '">' + name + '</option>';
						}
					});
					select += '</select>';
					return $(select).css(settings.cssStyle).prop(settings.attr).insertAfter($position).bind("change", function() {
						var $this = $(this);
						if ($this.val() == "") {
							var $prev = $this.prev("select[name='" + selectName + "']");
							if ($prev.size() > 0) {
								$input.val($prev.val());
							} else {
								$input.val(settings.emptyValue);
							}
						} else {
							$input.val($this.val());
						}
						addSelect($this, $this.val(), null);
					});
				}
			});
			
		}
	});
})(jQuery);